﻿Imports DBUtilitiesLibrary
Imports System.Data.SqlClient

Public Class UsernameRecovery
    Inherits System.Web.UI.Page
    Dim objDB As New DARSDBConnection
    Dim sql As String
    Dim lastName As String
    Dim IDNumber As String
    Dim userType As String
    Dim Username As String


    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        lastName = txtLastName.Text.Trim
        IDNumber = txtIDNumber.Text.Trim

        'Calls Functions to validate user input
        If Not ValidateEntries(lastName, IDNumber) Then
            lblUserNotFound.Text = "You must enter your last name and ID number."
            txtLastName.Focus()
            Exit Sub
        Else
            If Not RadioButtonCheck() Then
                lblUserNotFound.Text = "Please select if you are a student or advisor"
                Exit Sub
            Else
                userType = rdbUserType.SelectedValue
            End If
        End If

        GetUsername()

    End Sub

    'Checks to see if textbox inputs are empty
    Protected Function ValidateEntries(ByVal lastName As String, ByVal IDNumber As String) As Boolean
        If lastName = "" Or IDNumber = "" Then
            Return False
        Else
            Return True
        End If
    End Function

    'Checks to see if any radio button options were selected
    Protected Function RadioButtonCheck() As Boolean
        If rdbUserType.SelectedIndex = 0 Or rdbUserType.SelectedIndex = 1 Then
            Return True
        Else
            Return False
        End If
    End Function

    'Retrieves the username from the database that matches the last name and ID number
    Protected Sub GetUsername()
        Try
            objDB = New DARSDBConnection

            'Creates a select statement based on if the user is an advisor or a student
            If userType = "Advisor" Then
                sql = "SELECT * From Advisor WHERE AdvisorID = '" & IDNumber & "' AND LastName = '" & lastName & "'"
            ElseIf userType = "Student" Then
                sql = "SELECT * From Student WHERE StudentID = '" & IDNumber & "' AND LastName = '" & lastName & "'"
            End If

            Dim reader As SqlDataReader = objDB.getDataReader(sql)

            reader.Read()
            Dim username As String = CStr(reader.Item("UserID"))
            reader.Close()

            lblUserName.Text = "Your Username is: " & username
            pnlInput.Visible = False
        Catch ex As Exception
            lblUserNotFound.Text = "Username was not found. Please make sure the information is correct."
        End Try
    End Sub

    Protected Sub Welcome(ByVal sender As Object, ByVal e As EventArgs) Handles btnWelcomePage.ServerClick
        Response.Redirect("Welcome.aspx")
    End Sub
End Class